
from PySide.QtGui import *

from core.base import *
from shinylist import *

class CustomerView(BaseView):
	def show_add_customer_dialog(self):
		dialog = CustomerViewAddDialog(self)
		dialog.exec_()

	def on_data_update(self, customers):
		print 'CustomerView received update'
		slist = self.slist
		slist.clear()

		for customer in customers:
			item = ShinyListItem()
			item.set_main_text(customer.name)
			item.set_sub_text(customer.note)
			slist.add_item(item)

	def init_ui(self):
		customer_layout = QVBoxLayout() # The layout for the contents of the customer tab.
		timeframe_layout = QHBoxLayout() # The layout for the timeframe text and combo box
		customer_instruction = QLabel() # instructional text for viewing the customers
		# The button used to call the handler for adding customers into the database.
		self.add_customer_button = add_customer_button = QPushButton('Add a Customer', self) 
		customer_timeframe = QComboBox() # The combo box to handle the timeframe for the list.
		customer_caption = QLabel() # Caption for the customer timeframe
		slist = self.slist = ShinyList() # The list at the middle of the tab pane

		# Formatting the instructions label
		customer_instruction.setText('Click on an individual customer in the list to see all details about that customer and edit those details.')

		# Then we'll fix the combo box and it's accompanying caption
		customer_caption.setText('Show Customers from')
		customer_timeframe.addItem('2 weeks',None)
		customer_timeframe.addItem('1 month',None)
		customer_timeframe.addItem('2 months',None)
		customer_timeframe.addItem('6 months',None)
		customer_timeframe.addItem('1 year',None)
		customer_timeframe.addItem('Forever',None)

		# Now to integrate the timeframe widgets into one horizontal layout
		timeframe_layout.addWidget(customer_caption)
		timeframe_layout.addWidget(customer_timeframe)

		# Now to put them all together
		customer_layout.addWidget(customer_instruction)
		customer_layout.addLayout(timeframe_layout)
		customer_layout.addWidget(slist)
		customer_layout.addWidget(add_customer_button)

		# Set the layout of the view
		self.setLayout(customer_layout)

	def connect_signals_and_slots(self):
		self.add_customer_button.clicked.connect(self.show_add_customer_dialog)

	def __init__(self, parent=None, controller=None):
		super(CustomerView, self).__init__(parent, controller)
		self.init_ui()
		self.connect_signals_and_slots()


class CustomerViewAddDialog(QDialog):
	def _create_bottom_buttons(self):
		button_layout = QHBoxLayout()
		self.saveButton = saveButton = QPushButton('Save')
		self.cancelButton = cancelButton = QPushButton('Cancel')

		saveButton.clicked.connect(self.accept)
		cancelButton.clicked.connect(self.reject)

		button_layout.addStretch()
		button_layout.addWidget(saveButton)
		button_layout.addWidget(cancelButton)
		return button_layout

	def init_ui(self):
		self.top_layout = layout = QVBoxLayout()
		self.small_inputs_layout = small_inputs_layout = QGridLayout()

		nameLabel = QLabel('<b>Name</b>:')
		addrLabel = QLabel('<b>Address</b>:')
		contactLabel = QLabel('<b>Contact Number</b>:')
		noteLabel = QLabel('<b>Notes</b>:')

		self.nameEdit = nameEdit = QLineEdit()
		self.addrEdit = addrEdit = QLineEdit()
		self.contactEdit = contactEdit = QLineEdit()
		self.noteEdit = noteEdit = QTextEdit()
		
		small_inputs_layout.addWidget(nameLabel, 0, 0)
		small_inputs_layout.addWidget(addrLabel, 1, 0)
		small_inputs_layout.addWidget(contactLabel, 2, 0)

		small_inputs_layout.addWidget(nameEdit, 0, 1)
		small_inputs_layout.addWidget(addrEdit, 1, 1)
		small_inputs_layout.addWidget(contactEdit, 2, 1)

		layout.addLayout(small_inputs_layout)
		layout.addWidget(contactLabel)
		layout.addWidget(self.noteEdit)
		layout.addLayout(self._create_bottom_buttons())

		self.setLayout(layout)

	def __init__(self, parent=None):
		super(CustomerViewAddDialog, self).__init__(parent)
		self.setWindowTitle('Add Customer')
		self.init_ui()
		


def main():
	import sys
	app = QApplication(sys.argv)

	wnd = CustomerView()
	wnd.setWindowTitle('CustomerView')
	wnd.show()

	sys.exit(app.exec_())

if __name__ == '__main__':
	main()

